<?php
namespace app\admin\controller;
use app\admin\model\Store as StoreModel;
use app\admin\model\StoreApply;

use app\admin\traits\Result;
use think\facade\Request;
use app\common\model\Banner as BannerModel;


class Banner extends Common
{
    
    

    //列表
    public function index(){

        if ($this->request->isAjax()) {
            $data = [
                'key' => $this->request->get('key', '', 'trim'),
                'limit' => $this->request->get('limit', 10, 'intval'),
            ];

            $list = BannerModel::withSearch(['title'], ['title' => $data['key']])->order('id desc')
                ->paginate($data['limit'], false, ['query' => $data]);
            // dump(Zhuanqu::getlastsql());die;
            $user_date = [];
            foreach ($list as $key => $val) {
                $user_date[$key] = $val;
            }

            return show($user_date, 0, '', ['count' => $list->total()]);
        }
        return $this->fetch();

    }

    //验证是否存在
    public function bannercheck()
    {
        $username = $this->request->get('username', '', 'trim');
        $res = BannerModel::where('title', '=', $username)->field('title')->find();
        if ($res) {
            $msg = ['code' => 1, 'msg' => '名称已存在'];
        } else {
            $msg = ['status' => 0, 'info' => '验证通过'];
        }
        return $msg;
    }

    public function bannerdisable()
    {

        $id = input('post.id');
        if(!$id){
            $this->error('非法操作');
        }
        $is_show = input('post.is_show');
        $res = BannerModel::where('id='.$id)->update(['is_show'=>$is_show]);
        if ($res) {
            $msg = Result::success('操作成功');
        } else {
            $msg = Result::error('操作失败');
        }
        return $msg;

    }

    public function edit(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = validate('Banner');
            if (!$validate->check($data)) {
                //令牌数据无效时重置令牌
                $validate->getError() != '令牌数据无效' ? $token = Request::token() : $token = '';
                $msg = Result::error($validate->getError(), null, ['token' => $token]);
                return $msg;
            }

            if ($data['id']) {
                //编辑
                $res = BannerModel::update($data, ['id' => $data['id']]);
                if ($res) {
                    $msg = Result::success('编辑成功', url('/admin/pageset-zhuanqu'));
                } else {
                    $msg = Result::error('编辑失败');
                }
            } else {
                //添加
                $Banner = new BannerModel;
                $res = $Banner->allowField(true)->save($data);
                if ($res) {
                    $msg = Result::success('添加成功', url('/admin/pageset-zhuanqu'));
                } else {
                    $msg = Result::error('添加失败');
                }
            }
            return $msg;
        } else {
            $id = $this->request->get('id', 0, 'intval');
            if ($id) {
                $list = BannerModel::where('id', '=', $id)->find();

                $this->assign('list', $list);
            }

            return $this->fetch();
        }

    }


    public function del(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
            $res = BannerModel::where('id='.$id)->delete();
            if ($res) {
                $msg = Result::success('删除成功');
            } else {
                $msg = Result::error('删除失败');
            }

            return $msg;

        } else {
            $this->error('参数错误');
        }

    }
    
}



?>